/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.apache.log4j.Logger;

import who.tmap.bo.ThematicMap;
import who.tmap.ui.util.ThematicMapPropertiesUIHelper;

public class JPanelGlobalPreferences extends JPanel{
	private static Logger logger = Logger.getLogger(JDialogThematicMapProperties.class);	
		
	private JPanel jContentPane = null;
	private JPanel jPanelInfo = null;
	private JPanel jPanelBaseMap = null;
	private JLabel jLabelBaseMap = null;
	private JTextField jTextFieldBaseMap = null;
	private JButton jButtonBaseMapBrowse = null;
	private JPanel jPanelData = null;
	private JLabel jLabelData = null;
	private JTextField jTextFieldData = null;
	private JButton jButtonDataBrowse = null;
	private JPanel jPanelCD = null;
	private JLabel jLabelCD = null;
	private JTextField jTextFieldCD = null;
	private JButton jButtonCD = null;
	private JPanel jPanelAnnotations = null;
	private JLabel jLabelAnnotations = null;
	private JTextField jTextFieldAnnotations = null;
	private JButton jButtonAnnotations = null;
	private JPanel jPanelCenter = null;
	private JPanel jPanelMsg = null;
	private JLabel jLabelMsg1 = null;
	private JLabel jLabelMsg2 = null;
	private JPanelGlobalPreferences thisDialog =this;
		
	private ThematicMapPropertiesUIHelper tmpUIHelper =new ThematicMapPropertiesUIHelper();
	private ThematicMap thematicMap;
	//private boolean okPressed =false;

	private JPanel jPanelBackgroundImage = null;
	private JLabel jLabelBackgroundImage = null;
	private JTextField jTextFieldBackgroundImage = null;
	private JButton jButtonBackgroundImage = null;


	public JPanelGlobalPreferences( ThematicMap thematicMap ) {
		this.thematicMap =thematicMap;
		initialize();
	}

	private void initialize() {
		this.setSize(500, 340);			
		this.add(getJContentPane());			
	}

	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanelInfo(), java.awt.BorderLayout.NORTH);
			jContentPane.add(getJPanelCenter(), java.awt.BorderLayout.CENTER);
		}
		return jContentPane;
	}

	
	private String getTextVal( JTextField jTextField ) {
		return !jTextField.getText().equals("") ? jTextField.getText() 
												: null;
	}

	private JPanel getJPanelInfo() {
		if (jPanelInfo == null) {
			GridLayout gridLayout = new GridLayout(6,1);
			gridLayout.setHgap(5);
			gridLayout.setVgap(5);
			jPanelInfo = new JPanel();
			jPanelInfo.setBorder(javax.swing.BorderFactory.createEmptyBorder(5,5,5,5));
			jPanelInfo.setLayout(gridLayout);
			jPanelInfo.add(getJPanelMsg(), null);
			jPanelInfo.add(getJPanelBaseMap(), null);
			jPanelInfo.add(getJPanelData(), null);
			jPanelInfo.add(getJPanelCD(), null);
			jPanelInfo.add(getJPanelAnnotations(), null);
			jPanelInfo.add(getJPanelBackgroundImage(), null);
		}
		return jPanelInfo;
	}

	private JPanel getJPanelBaseMap() {
		if (jPanelBaseMap == null) {
			jPanelBaseMap =new JPanel( new GridBagLayout() );
			GridBagConstraints cc =new GridBagConstraints();
			
			jLabelBaseMap =new JLabel( "Base Map" );
			cc.gridx =0;
			cc.gridy =0;
			cc.gridheight =1;
			cc.gridwidth =1;
			cc.weightx =.5;
			cc.weighty =.0;
			cc.insets =new Insets(5,5,5,5);
			cc.anchor =GridBagConstraints.NORTHWEST;
			cc.fill =GridBagConstraints.HORIZONTAL;
			jPanelBaseMap.add( jLabelBaseMap,cc );
			
			
			cc.gridx =0;
			cc.gridy =1;
			cc.gridheight =1;
			cc.gridwidth =1;
			cc.weightx =.5;
			cc.weighty =.0;
			cc.insets =new Insets(5,5,5,5);
			cc.anchor =GridBagConstraints.NORTHWEST;
			cc.fill =GridBagConstraints.HORIZONTAL;
			jPanelBaseMap.add( getJTextFieldBaseMap(), cc);
			
			cc.gridx =1;
			cc.gridy =1;
			cc.gridheight =1;
			cc.gridwidth =1;
			cc.weightx =.0;
			cc.weighty =.0;
			cc.insets =new Insets(5,5,5,5);
			cc.anchor =GridBagConstraints.NORTHWEST;
			cc.fill =GridBagConstraints.NONE;
			jPanelBaseMap.add(getJButtonBaseMapBrowse(),cc);
			
			
			
			/*BorderLayout borderLayout = new BorderLayout();
			borderLayout.setHgap(5);
			borderLayout.setVgap(5);
			jLabelBaseMap = new JLabel();
			jLabelBaseMap.setText("Base map");
			jPanelBaseMap = new JPanel();
			jPanelBaseMap.setLayout(borderLayout);
			jPanelBaseMap.add(jLabelBaseMap, java.awt.BorderLayout.LINE_START);
			jPanelBaseMap.add(getJTextFieldBaseMap(), java.awt.BorderLayout.CENTER);
			jPanelBaseMap.add(getJButtonBaseMapBrowse(), java.awt.BorderLayout.LINE_END);*/
		}
		return jPanelBaseMap;
	}

	private JTextField getJTextFieldBaseMap() {
		if (jTextFieldBaseMap == null) {
			jTextFieldBaseMap = new JTextField();
			jTextFieldBaseMap.setText( thematicMap.getBaseMapLinkedFileName() != null ? thematicMap.getBaseMapLinkedFileName() : "" );
			jTextFieldBaseMap.setMaximumSize( new Dimension( 100 , 20 ));
			jTextFieldBaseMap.setPreferredSize( new Dimension( 100 , 20 ));
		}
		return jTextFieldBaseMap;
	}

	private JButton getJButtonBaseMapBrowse() {
		if (jButtonBaseMapBrowse == null) {
			jButtonBaseMapBrowse = new JButton();
			jButtonBaseMapBrowse.setText("Browse...");
			jButtonBaseMapBrowse.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					File file =tmpUIHelper.getBaseMapFile( thisDialog );
					if ( file != null ) {
						thematicMap.setBaseMapLinkedFileName( file.getAbsolutePath() );
						thisDialog.getJTextFieldBaseMap().setText( thematicMap.getBaseMapLinkedFileName() );
					}
				}
			});
		}
		return jButtonBaseMapBrowse;
	}

	private JPanel getJPanelData() {
		if (jPanelData == null) {
			jPanelData =new JPanel( new GridBagLayout()  );
			GridBagConstraints cc=new GridBagConstraints();
			
			jLabelData = new JLabel();
			jLabelData.setText("Data");
			cc.gridx =0;
			cc.gridy =0;
			cc.gridheight =1;
			cc.gridwidth =1;
			cc.weightx =.5;
			cc.weighty =.0;
			cc.insets =new Insets(5,5,5,5);
			cc.anchor =GridBagConstraints.NORTHWEST;
			cc.fill =GridBagConstraints.HORIZONTAL;
			jPanelData.add( jLabelData, cc );
			
			cc.gridx =0;
			cc.gridy =1;
			cc.gridheight =1;
			cc.gridwidth =1;
			cc.weightx =.5;
			cc.weighty =.0;
			cc.insets =new Insets(5,5,5,5);
			cc.anchor =GridBagConstraints.NORTHWEST;
			cc.fill =GridBagConstraints.HORIZONTAL;
			jPanelData.add( getJTextFieldData(), cc);
			
			cc.gridx =1;
			cc.gridy =1;
			cc.gridheight =1;
			cc.gridwidth =1;
			cc.weightx =.0;
			cc.weighty =.0;
			cc.insets =new Insets(5,5,5,5);
			cc.anchor =GridBagConstraints.NORTHWEST;
			cc.fill =GridBagConstraints.NONE;
			jPanelData.add( getJButtonDataBrowse(), cc );
			
		/*	BorderLayout borderLayout1 = new BorderLayout();
			borderLayout1.setHgap(5);
			borderLayout1.setVgap(5);
			jPanelData = new JPanel();
			jPanelData.setLayout(borderLayout1);
			jPanelData.add(jLabelData, java.awt.BorderLayout.LINE_START);
			jPanelData.add(getJTextFieldData(), java.awt.BorderLayout.CENTER);
			jPanelData.add(getJButtonDataBrowse(), java.awt.BorderLayout.LINE_END);*/
		}
		return jPanelData;
	}

	private JTextField getJTextFieldData() {
		if (jTextFieldData == null) {
			jTextFieldData = new JTextField();
			jTextFieldData.setText( thematicMap.getDataLinkedFileName() != null ? thematicMap.getDataLinkedFileName() : "" );				
			jTextFieldData.setMaximumSize( new Dimension( 100 , 20 ));
			jTextFieldData.setPreferredSize( new Dimension( 100 , 20 ));
		}
		return jTextFieldData;
	}

	private JButton getJButtonDataBrowse() {
		if (jButtonDataBrowse == null) {
			jButtonDataBrowse = new JButton();
			jButtonDataBrowse.setText("Browse...");
			jButtonDataBrowse.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					File file =tmpUIHelper.getDataFile( thisDialog );
					if ( file != null ) {
						thematicMap.setDataLinkedFileName( file.getAbsolutePath() );
						thisDialog.getJTextFieldData().setText( thematicMap.getDataLinkedFileName() );
					}
				}
			});
		}
		return jButtonDataBrowse;
	}

	private JPanel getJPanelCD() {
		if (jPanelCD == null) {
			
			jPanelCD =new JPanel( new GridBagLayout()  );
			GridBagConstraints cc=new GridBagConstraints();
			
			jLabelCD = new JLabel();
			jLabelCD.setText("Cartographic Display");
			cc.gridx =0;
			cc.gridy =0;
			cc.gridheight =1;
			cc.gridwidth =1;
			cc.weightx =.5;
			cc.weighty =.0;
			cc.insets =new Insets(5,5,5,5);
			cc.anchor =GridBagConstraints.NORTHWEST;
			cc.fill =GridBagConstraints.HORIZONTAL;
			jPanelCD.add( jLabelCD, cc );
			
			cc.gridx =0;
			cc.gridy =1;
			cc.gridheight =1;
			cc.gridwidth =1;
			cc.weightx =.5;
			cc.weighty =.0;
			cc.insets =new Insets(5,5,5,5);
			cc.anchor =GridBagConstraints.NORTHWEST;
			cc.fill =GridBagConstraints.HORIZONTAL;
			jPanelCD.add( getJTextFieldCD(), cc);
			
			cc.gridx =1;
			cc.gridy =1;
			cc.gridheight =1;
			cc.gridwidth =1;
			cc.weightx =.0;
			cc.weighty =.0;
			cc.insets =new Insets(5,5,5,5);
			cc.anchor =GridBagConstraints.NORTHWEST;
			cc.fill =GridBagConstraints.NONE;
			jPanelCD.add( getJButtonCD(), cc );
			/*
			BorderLayout borderLayout2 = new BorderLayout();
			borderLayout2.setHgap(5);
			borderLayout2.setVgap(5);
			jPanelCD = new JPanel();
			jPanelCD.setLayout(borderLayout2);
			jPanelCD.add(jLabelCD, java.awt.BorderLayout.LINE_START);
			jPanelCD.add(getJTextFieldCD(), java.awt.BorderLayout.CENTER);
			jPanelCD.add(getJButtonCD(), java.awt.BorderLayout.LINE_END);*/
		}
		return jPanelCD;
	}

	private JTextField getJTextFieldCD() {
		if (jTextFieldCD == null) {
			jTextFieldCD = new JTextField();
			jTextFieldCD.setText( thematicMap.getCartographicDisplayLinkedFileName() != null ? thematicMap.getCartographicDisplayLinkedFileName() : "" );
			jTextFieldCD.setPreferredSize( new Dimension( 100 , 20 ) );
			jTextFieldCD.setMaximumSize( new Dimension( 100 , 20 ) );
		}
		return jTextFieldCD;
	}

	private JButton getJButtonCD() {
		if (jButtonCD == null) {
			jButtonCD = new JButton();
			jButtonCD.setText("Browse...");
			jButtonCD.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					File file =tmpUIHelper.getCartographicDisplayFile( thisDialog );
					if ( file != null ) {
						thematicMap.setCartographicDisplayLinkedFileName( file.getAbsolutePath() );
						thisDialog.getJTextFieldCD().setText( thematicMap.getCartographicDisplayLinkedFileName() );
					}
				}
			});
		}
		return jButtonCD;
	}

	private JPanel getJPanelAnnotations() {
		if (jPanelAnnotations == null) {
			
			jPanelAnnotations =new JPanel( new GridBagLayout()  );
			GridBagConstraints cc=new GridBagConstraints();
			
			jLabelAnnotations = new JLabel();
			jLabelAnnotations.setText("Annotations");
			cc.gridx =0;
			cc.gridy =0;
			cc.gridheight =1;
			cc.gridwidth =1;
			cc.weightx =.5;
			cc.weighty =.0;
			cc.insets =new Insets(5,5,5,5);
			cc.anchor =GridBagConstraints.NORTHWEST;
			cc.fill =GridBagConstraints.HORIZONTAL;
			jPanelAnnotations.add( jLabelAnnotations, cc );
			
			cc.gridx =0;
			cc.gridy =1;
			cc.gridheight =1;
			cc.gridwidth =1;
			cc.weightx =.5;
			cc.weighty =.0;
			cc.insets =new Insets(5,5,5,5);
			cc.anchor =GridBagConstraints.NORTHWEST;
			cc.fill =GridBagConstraints.HORIZONTAL;
			jPanelAnnotations.add( getJTextFieldAnnotations(), cc);
			
			cc.gridx =1;
			cc.gridy =1;
			cc.gridheight =1;
			cc.gridwidth =1;
			cc.weightx =.0;
			cc.weighty =.0;
			cc.insets =new Insets(5,5,5,5);
			cc.anchor =GridBagConstraints.NORTHWEST;
			cc.fill =GridBagConstraints.NONE;
			jPanelAnnotations.add( getJButtonAnnotations(), cc );
			/*
			jLabelAnnotations = new JLabel();
			jLabelAnnotations.setText("Annotations");
			BorderLayout borderLayout3 = new BorderLayout();
			borderLayout3.setHgap(5);
			borderLayout3.setVgap(5);
			jPanelAnnotations = new JPanel();
			jPanelAnnotations.setLayout(borderLayout3);
			jPanelAnnotations.add(jLabelAnnotations, java.awt.BorderLayout.LINE_START);
			jPanelAnnotations.add(getJTextFieldAnnotations(), java.awt.BorderLayout.CENTER);
			jPanelAnnotations.add(getJButtonAnnotations(), java.awt.BorderLayout.LINE_END);*/
		}
		return jPanelAnnotations;
	}

	private JTextField getJTextFieldAnnotations() {
		if (jTextFieldAnnotations == null) {
			jTextFieldAnnotations = new JTextField();
			jTextFieldAnnotations.setText( thematicMap.getAnnotationsLinkedFileName() != null ? thematicMap.getAnnotationsLinkedFileName() : "" );
			jTextFieldAnnotations.setPreferredSize( new Dimension( 100 , 20 ) );
			jTextFieldAnnotations.setMaximumSize( new Dimension( 100 , 20 ) );
		}
		return jTextFieldAnnotations;
	}

	private JButton getJButtonAnnotations() {
		if (jButtonAnnotations == null) {
			jButtonAnnotations = new JButton();
			jButtonAnnotations.setText("Browse...");
			jButtonAnnotations.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					File file =tmpUIHelper.getAnnotationsFile( thisDialog );
					if ( file != null ) {
						thematicMap.setAnnotationsLinkedFileName( file.getAbsolutePath() );
						thisDialog.getJTextFieldAnnotations().setText( thematicMap.getAnnotationsLinkedFileName() );
					}
				}
			});
		}
		return jButtonAnnotations;
	}

	private JPanel getJPanelCenter() {
		if (jPanelCenter == null) {
			jPanelCenter = new JPanel();
		}
		return jPanelCenter;
	}

	private JPanel getJPanelMsg() {
		if (jPanelMsg == null) {
			jPanelMsg =new JPanel( new BorderLayout() );	
			jLabelMsg1 = new JLabel();
			StringBuilder message =new StringBuilder();
			message.append( "<HTML>" );
			message.append( " When using the .tml file format, the thematic map can be linked to external files<BR>");
			message.append( " instead of embedding data as it is done with the .tma file format." );
			message.append( "</HTML>" );
			
			jLabelMsg1.setText( message.toString() );
			jPanelMsg.add( jLabelMsg1 , BorderLayout.CENTER );
		/*	GridLayout gridLayout1 = new GridLayout();
			gridLayout1.setRows(2);
			jLabelMsg1 = new JLabel();
			jLabelMsg1.setText("When using the .tml file format, the thematic map can be linked to external files,");
			jLabelMsg2 = new JLabel();
			jLabelMsg2.setText("instead of embedding data as it is done with the .tma file format.");			
			jPanelMsg = new JPanel();
			jPanelMsg.setLayout(gridLayout1);
			jPanelMsg.add(jLabelMsg1, null);
			jPanelMsg.add(jLabelMsg2, null);*/
		}
		return jPanelMsg;
	}

/*	public boolean isOkPressed() {
		return okPressed;
	}*/

	private JPanel getJPanelBackgroundImage() {
		if (jPanelBackgroundImage == null) {

			jPanelBackgroundImage =new JPanel( new GridBagLayout()  );
			GridBagConstraints cc=new GridBagConstraints();
			
			jLabelBackgroundImage = new JLabel();
			jLabelBackgroundImage.setText("Background image");
			cc.gridx =0;
			cc.gridy =0;
			cc.gridheight =1;
			cc.gridwidth =1;
			cc.weightx =.5;
			cc.weighty =.0;
			cc.insets =new Insets(5,5,5,5);
			cc.anchor =GridBagConstraints.NORTHWEST;
			cc.fill =GridBagConstraints.HORIZONTAL;
			jPanelBackgroundImage.add( jLabelBackgroundImage, cc );
			
			cc.gridx =0;
			cc.gridy =1;
			cc.gridheight =1;
			cc.gridwidth =1;
			cc.weightx =.5;
			cc.weighty =.0;
			cc.insets =new Insets(5,5,5,5);
			cc.anchor =GridBagConstraints.NORTHWEST;
			cc.fill =GridBagConstraints.HORIZONTAL;
			jPanelBackgroundImage.add( getJTextFieldBackgroundImage(), cc );
			
			cc.gridx =1;
			cc.gridy =1;
			cc.gridheight =1;
			cc.gridwidth =1;
			cc.weightx =.0;
			cc.weighty =.0;
			cc.insets =new Insets(5,5,5,5);
			cc.anchor =GridBagConstraints.NORTHWEST;
			cc.fill =GridBagConstraints.NONE;
			jPanelBackgroundImage.add( getJButtonBackgroundImage(), cc );
			
			
		/*	jLabelBackgroundImage = new JLabel();
			jLabelBackgroundImage.setText("Background image");
			BorderLayout borderLayout3 = new BorderLayout();
			borderLayout3.setHgap(5);
			borderLayout3.setVgap(5);
			jPanelBackgroundImage = new JPanel();
			jPanelBackgroundImage.setLayout(borderLayout3);
			jPanelBackgroundImage.add(jLabelBackgroundImage, java.awt.BorderLayout.LINE_START);
			jPanelBackgroundImage.add(getJTextFieldBackgroundImage(), BorderLayout.CENTER);
			jPanelBackgroundImage.add(getJButtonBackgroundImage(), java.awt.BorderLayout.LINE_END);				*/
			
		}
		return jPanelBackgroundImage;
	}

	private JTextField getJTextFieldBackgroundImage() {
		if (jTextFieldBackgroundImage == null) {
			jTextFieldBackgroundImage = new JTextField();
			jTextFieldBackgroundImage.setText( thematicMap.getBackgroundImageLinkedFileName() != null ? thematicMap.getBackgroundImageLinkedFileName() : "" );
			jTextFieldBackgroundImage.setPreferredSize( new Dimension( 100 , 20 ) );
			jTextFieldBackgroundImage.setMaximumSize( new Dimension( 100 , 20 ) );
		}
		return jTextFieldBackgroundImage;
	}

	private JButton getJButtonBackgroundImage() {
		if (jButtonBackgroundImage == null) {
			jButtonBackgroundImage = new JButton();
			jButtonBackgroundImage.setText( "Browse..." );
			jButtonBackgroundImage.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					File file =tmpUIHelper.getBackgroundImageFile( thisDialog );
					if ( file != null ) {
						thematicMap.setBackgroundImageLinkedFileName( file.getAbsolutePath() );
						thisDialog.getJTextFieldBackgroundImage().setText( thematicMap.getBackgroundImageLinkedFileName() );
					}
				}
			});
		}
		return jButtonBackgroundImage;
	}

	public void save() {
		thematicMap.setBaseMapLinkedFileName( getTextVal( thisDialog.getJTextFieldBaseMap() ) );
		thematicMap.setDataLinkedFileName( getTextVal( thisDialog.getJTextFieldData() ) );	
		thematicMap.setCartographicDisplayLinkedFileName( getTextVal( thisDialog.getJTextFieldCD() ) );	
		thematicMap.setAnnotationsLinkedFileName( getTextVal( thisDialog.getJTextFieldAnnotations() ) );
		thematicMap.setBackgroundImageLinkedFileName( getTextVal( thisDialog.getJTextFieldBackgroundImage() ) );
	}

}
